package org.tobuy.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.tobuy.factory.UserDAOFactory;
import org.tobuy.vo.User;

public class LoginServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String path = "login.jsp";
		String userid = req.getParameter("userid");
		String userpass = req.getParameter("userpass");
		List<String> info = new ArrayList<String>();  // collect information
		if(userid==null || "".equals(userid)){
			info.add("id can not be empty!!");
		}
		if(userpass==null || "".equals(userpass)){
			info.add("password can not be empty!!");
		}
		if(info.size()==0){  // no mistake
			User user = new User();
			user.setUser_id(userid);
			user.setUser_pass(userpass);
			try{
				if(UserDAOFactory.getIUserDAOInstance().findLogin(user)){
					req.setAttribute("user", user);
				}else{
					user=null;
					req.setAttribute("user", user);
					info.add("Wrong combination of ID and password! Please try again!");
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		req.setAttribute("info", info);
		req.getRequestDispatcher(path).forward(req, resp);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);

		
	}

}
